package com.iuie.ns.frame.aop;

import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

import com.iuie.ns.frame.exception.ServiceException;
import com.iuie.ns.frame.web.response.vo.ResponseInfo;

/**
 * @desc 处理ServiceException的切面
 * @createTime 2017年6月25日 下午2:49:11
 * @author iuie
 */
@ControllerAdvice
public class ServiceExceptionHandler {
	
    // 指定要try catch的异常
    @ExceptionHandler(value = Exception.class)
    @ResponseBody
    public String handle(Exception e) {
        e.printStackTrace();
        String str = e.getMessage();
        if (e instanceof ServiceException) {
            ServiceException ex = (ServiceException) (e);
            String code = ex.getCode();
            Object info = ex.getInfo();
            return ResponseInfo.errorJson(str, code, info);
        }
        return ResponseInfo.errorJson(e.getMessage());
    }

}
